﻿using System;
using System.Collections.Generic;
using System.Linq;
using FineUI;
using SysManage.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;


namespace SysManage.Module.Possy
{
    public partial class PartyPossyList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyPossyInfoView";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据加载
        private void BindData()
        {
            // 权限检查
            CheckPowerWithButton("PartyPossyInfoAdd", btnAdd);
            btnAdd.OnClientClick = Dialog.GetShowReference("PartyPossyEdit.aspx", "新增");
            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();
            this.ppBranch.WindinsUrl = "../../../Share/Picker/BranchSinglePicker.aspx?BranchID=" + BranchID;
            this.ppBranch.Value = BranchID;
            this.ppBranch.Text = ControlBusiness.GetBranchPickerText(BranchID);
            this.ppBranch.ShowTrigger1 = false;
        }
        private void LoadData()
        {
            var ds = PartyPossyInfo.GetGridData(Grid1.PageIndex, Grid1.PageSize, ddlIsShowPortal.SelectedValue,this.ppBranch.Value,
                txtPossyName.Text);

            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithWindowField("PartyPossyInfoEdit", Grid1, "editField");
            CheckPowerWithLinkButtonField("PartyPossyInfoDelete", Grid1, "deleteField");
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            LoadData();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string PartyPossyInfoId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("PartyPossyInfoDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }

             

                var del = DB.PartyPossyInfos.FirstOrDefault(t => t.ID == PartyPossyInfoId);
                DB.PartyPossyInfos.Remove(del);
                DB.SaveChanges();
                LoadData();
            }
        }

        public static string GetBranchName(string branchId)
        {
            return ControlBusiness.GetBranchPickerText(branchId);
        }

        #endregion

        #region 按钮事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("PartyPossyInfoDelete"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyPossyInfos.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyPossyInfos.Remove(u));
            DB.SaveChanges();

            // 重新绑定表格
            LoadData();
        }
        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            LoadData();
        }


        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            LoadData();
        }
    }


}